import { fileServerPath } from '@/config/app'
import { Loading } from 'element-ui'
import { toTree } from '../jcore/tree'

/**
 * 清除空的键
 * @param {any} data
 * @returns {any}
 */
export function clearEmptyKey(data) {
  let result = {}
  for (let key in data) {
    if (data[key] === 0 || data[key]) {
      result[key] = data[key]
    }
  }
  return result
}
/**
 * 页面加载样式
 * @param {any} options
 * @returns
 */
export function pageLoading(options) {
  const defaultOption = {
    lock: true,
    text: 'Loading',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  }
  options = Object.assign({}, defaultOption, options)
  return Loading.service(options)
}
/**
 * 表单验证 promise
 * @param {any} vm vue
 * @param {string} formName 表单名
 * @returns
 */
export function formValidate(vm, formName) {
  return new Promise((resolve, reject) => {
    vm.$refs[formName].validate((valid) => {
      if (valid) {
        resolve(valid)
      } else {
        reject(valid)
      }
    })
  })
}
/**
 * 扁平数据添加父级
 * @param {any[]} list
 * @returns
 */
export function flatListAddPid(list) {
  return list.map((e) => {
    const s = e.hierarchy || '/rt-user'
    const isRoot = s == '/'
    e.mid = isRoot ? '/' : s
    e.pid = isRoot ? '0' : s.substring(0, s.length - 5)
    return e
  })
}
/**
 * 扁平数据添加父级并转树
 * @param {any[]} list
 * @returns
 */
export function flatPidToTree(list) {
  return toTree(flatListAddPid(list), { id: 'mid' })
}
/**
 * 需要展示树层级id
 * @param {any[]} treeList
 * @param {string} key
 * @param {number} level
 */
export function showTreeLayKeys(treeList, level = 1, key = 'id') {
  if (!Array.isArray(treeList)) return []
  let result = []
  const read = (list, lv) => {
    if (lv > level) return
    for (let item of list) {
      if (key in item) {
        result.push(item[key])
      }
      if (item.children && item.children.length) {
        read(item.children, lv + 1)
      }
    }
  }
  read(treeList, 1)
  return result
}
/**
 * blob 文件流下载
 * @param {Blob} blob
 * @param {string} fileName
 */
export function downLoadClick(blob, fileName) {
  const elink = document.createElement('a')
  if (fileName) {
    elink.download = fileName
  } else {
    elink.setAttribute()
  }
  elink.style.display = 'none'
  var href = URL.createObjectURL(blob)
  elink.href = href
  document.body.appendChild(elink)
  elink.click()
  URL.revokeObjectURL(elink.href) // 释放URL 对象
  document.body.removeChild(elink)
}
/**显示图片全路径 */
export function getFileAllPath(fid) {
  if (/^http/.test(fid)) return fid
  return fid ? `${fileServerPath}/oss/downloadFile?fid=${fid}` : ''
}
/**
 * 点击按钮上传文件
 * @param {string} accept
 * @param {boolean} multiple
 * @returns {Promise<File[]>}
 */
export function clickUploadFile(accept, multiple) {
  const input = document.createElement('input')
  input.type = 'file'
  if (accept) {
    input.accept = accept
  }
  if (multiple) {
    input.multiple = 'multiple'
  }
  input.click()
  return new Promise((resolve, reject) => {
    input.onchange = (f) => {
      resolve(f.target.files)
    }
    input.onerror = reject
  })
}
/**
 * 判断时间段是否重叠
 * @param {Array<Date[]>} timeArray
 * @returns
 */
export function isAnyOverlap(timeArray) {
  for (var i = 0; i < timeArray.length; i++) {
    for (var j = i + 1; j < timeArray.length; j++) {
      if (timeArray[i][0] < timeArray[j][1] && timeArray[i][1] > timeArray[j][0]) {
        return {
          isOverlap: true,
          index: i
        }
      }
    }
  }
  return null
}
